package top.wenzz.manage.system.manage.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import top.wenzz.commons.annotation.Edit;
import top.wenzz.commons.annotation.Query;
import top.wenzz.commons.entity.DataEntity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.sql.Timestamp;

/**
 * Created by cui on 2017/7/10.
 */
@Data
@Entity( name = "sys_user" )
public class SysUser extends DataEntity<SysUser> {
    @Column(name="username", length = 30, unique = true, nullable = false)
    @Length(min = 2, max = 30, message = "用户名长度为2-30")
    @NotBlank(message = "用户名不能为空")
    @Edit("用户名")
    @Query(Query.ReadType.LIKE)
    private String username;// 登陆用户名

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @Edit(value = "密码", update = false)
    private String password;// 密码

    @JsonIgnore
    private String salt;// 加密盐

    @Column(name="account_expired_time")
    @JsonFormat( pattern = "yyyy-MM-dd HH:mm:ss" )
    private Timestamp accountExpiredTime;// 账户过期时间

    @Column(name="credentials_expired_time")
    @JsonFormat( pattern = "yyyy-MM-dd HH:mm:ss" )
    private Timestamp credentialsExpiredTime;// 密码过期时间

    @Column(name="account_locked_time")
    @JsonFormat( pattern = "yyyy-MM-dd HH:mm:ss" )
    private Timestamp accountLockedTime;// 账户锁定时间

    @Edit("账户状态")
    private int enable;// 账户是否可用

    @ManyToOne
    @NotNull(message = "角色不能为空")
    @Edit("角色")
    @Query
    private SysRole role;

    @Column(nullable = false)
    @NotBlank(message = "姓名不能为空")
    @Pattern(regexp = "^[\u4e00-\u9fa5]{2,4}$", message = "姓名只允许中文，且长度为2-4")
    @Query(Query.ReadType.LIKE)
    @Edit
    private String name;

    @NotBlank(message = "性别不能为空")
    @Pattern(regexp = "^[0-1]$", message = "性别不正确")
    @Edit
    private String sex;

    @Edit("员工号")
    @Column(name="staff_id", length = 10)
    @Length(max = 10, message = "员工号最大长度为10")
    private String staffId;

    public SysUser() {
    }

    @Builder
    public SysUser(int id, int version, Timestamp createTime, String createUserId, Timestamp updateTime, String updateUserId, String username, String password, String salt, Timestamp accountExpiredTime, Timestamp credentialsExpiredTime, Timestamp accountLockedTime, int enable, SysRole role, String name, String sex, String staffId) {
        super(id, version, createTime, createUserId, updateTime, updateUserId);
        this.username = username;
        this.password = password;
        this.salt = salt;
        this.accountExpiredTime = accountExpiredTime;
        this.credentialsExpiredTime = credentialsExpiredTime;
        this.accountLockedTime = accountLockedTime;
        this.enable = enable;
        this.role = role;
        this.name = name;
        this.sex = sex;
        this.staffId = staffId;
    }
}
